package com.pcbly.web.components.form;

import java.util.ArrayList;
import java.util.List;

import com.pcbly.web.bean.table.ColumnBean;

public class AnalyseCommonDataExtCreate extends AbstractAnalyseCommonDataExt {
	public AnalyseCommonDataExtCreate() {

	}

	public StringBuilder execute() {
		StringBuilder sb = new StringBuilder();
		if (!columnList.isEmpty() && columnList.size() > 1) {
			int i = 0;
			sb.append("<tr>\r\n");
			for (ColumnBean column : columnList) {
				if (i != 0 && i % 2 == 0) {
					sb.append("</tr>\r\n<tr>\r\n");
				}
				if (column.getColumnStyle().equals("textarea")) {
					if (i != 0 && i % 2 != 0) {
						sb.append("	<td>");
						sb.append("</td>");
						sb.append("<td>");
						sb.append("</td>\r\n");
						sb.append("</tr>\r\n<tr>\r\n");
						i++;
					}
					sb.append("	<td>");
					sb.append(column.getColumnCName());
					sb.append("</td>\r\n");
					sb.append("	<td colspan=\"3\"  class=\"td-input\">");
					sb.append(getXml(column, false));
					sb.append("</td>\r\n");
					// sb.append("</tr>\r\n<tr>\r\n");
					i++;
				} else {
					sb.append("	<td>");
					sb.append(column.getColumnCName());
					sb.append("</td>\r\n");
					sb.append("	<td  class=\"td-input\">");
					sb.append(getXml(column, false));
					sb.append("</td>\r\n");
				}

				i++;
			}
			if (!(i % 2 == 0)) {
				sb.append("	<td>");
				sb.append("&nbsp;</td>");
				sb.append("	<td>");
				sb.append("&nbsp;</td>\r\n");
			}
			sb.append("</tr>\r\n");
		} else if (!columnList.isEmpty()) {
			ColumnBean column = columnList.get(0);
			sb.append("<tr>\r\n");
			sb.append("	<td>");
			sb.append(column.getColumnCName());
			sb.append("</td>\r\n");
			sb.append("	<td colspan=\"3\"  class=\"td-input\">");
			sb.append(getXml(column, false));
			sb.append("</td>\r\n");
			sb.append("</tr>\r\n");
		}
//		System.out.println(sb);
		return sb;
	}

	public static void main(String[] args) {
		AnalyseCommonDataExtCreate ext = new AnalyseCommonDataExtCreate();
		ColumnBean bean = new ColumnBean();
		List<ColumnBean> columnList = new ArrayList<ColumnBean>();

		bean.setColumnCName("textarea");
		bean.setColumnName("textarea");
		bean.setColumnStyle("textarea");
		bean.setColumnType("varchar");
		bean.setLength(511);
		bean.setColumnNull("1");
		columnList.add(bean);
		for (int i = 0; i < 5; i++) {
			bean = new ColumnBean();
			bean.setColumnCName("test" + i);
			bean.setColumnName("test" + i);
			bean.setColumnStyle("text");
			bean.setColumnType("varchar");
			bean.setLength(63);
			bean.setColumnNull("0");
			columnList.add(bean);
		}
		// for(int i=0;i<5;i++){
		// bean = new ColumnBean();
		// bean.setColumnCName("date"+i);
		// bean.setColumnName("date"+i);
		// bean.setColumnStyle("text");
		// bean.setColumnType("datetime");
		// bean.setColumnNull("0");
		// columnList.add(bean);
		// }
		for (int i = 0; i < 5; i++) {
			bean = new ColumnBean();
			bean.setColumnCName("textarea" + i);
			bean.setColumnName("textarea" + i);
			bean.setColumnStyle("textarea");
			bean.setLength(511);
			bean.setColumnNull("0");
			columnList.add(bean);
		}
		ext.setColumnList(columnList);
		ext.execute();
	}
}
